package com.zte.sys.dept.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zte.sys.area.model.SysCity;
import com.zte.sys.area.model.SysProvince;
import com.zte.sys.dept.dao.IDeptDao;
import com.zte.sys.dept.model.Dept;
import com.zte.sys.dept.model.DeptCondition;
import com.zte.sys.dept.model.SysCityCondition;
import com.zte.sys.dept.service.IDeptService;
import com.zte.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DeptService implements IDeptService {

    @Autowired
    private IDeptDao deptDao;

    /**
     * 分页查询
     * @param conn
     * @param pageModel
     * @return
     */
    @Override
    public PageModel<Dept> queryDeptPageByCondition(DeptCondition conn, PageModel<Dept> pageModel) {
        if (conn.getDeptNamelike().equals("null")){
            conn.setDeptNamelike("");
        }
        if (conn.getPId().equals("null") ){
            conn.setPId("");
        }
        if (conn.getCId().equals("null") ){
            conn.setCId("");
        }
        int pageNum = pageModel.getPageNum();
        pageNum = pageNum<=0?1:pageNum;
        int pageSize = 3;
        PageHelper.startPage(pageNum,pageSize);
        List<Dept> depts = deptDao.queryDeptListByCondition(conn);
        PageInfo<Dept> info = new PageInfo<Dept>(depts);

        pageModel.setPageNum(info.getPageNum());
        pageModel.setPageSize(info.getPageSize());
        pageModel.setTotalPage(info.getPages());
        pageModel.setTotalCount((int)info.getTotal());
        pageModel.setData(new ArrayList<Dept>(info.getList()));
        return pageModel;
    }

    /**
     * 查询所有省份
     * @param pId
     * @return
     */
    @Override
    public List<SysProvince> queryProvinceList(String pId) {
        return deptDao.queryProvinceList(pId);
    }

    /**
     * 查询所有市区
     * @param conn
     * @return
     */
    @Override
    public List<SysCity> queryCityList(SysCityCondition conn) {
        return deptDao.queryCityList(conn);
    }

    /**
     * 部门详情
     * @param conn
     * @return
     */
    @Override
    public Dept queryDeptByDeptId(DeptCondition conn) {
        return deptDao.queryDeptByDeptId(conn);
    }

    /**
     * 添加部门
     * @param dept
     * @return
     */
    @Override
    public int addDept(Dept dept) {
        dept.setDeptState("1");
        return deptDao.addDept(dept);
    }

    /**
     * 修改部门
     * @param dept
     * @return
     */
    @Override
    public int updateDept(Dept dept) {
        return deptDao.updateDept(dept);
    }

    /**
     * 修改部门状态
     * @param dept
     * @return
     */
    @Override
    public int updateDeptState(Dept dept) {
        if(dept.getDeptState().equals("1")){
            dept.setDeptState("2");
        }else{
            dept.setDeptState("1");
        }
        return deptDao.updateDeptState(dept);
    }
}
